/*
 * @Author: xuetao 3168581151@qq.com
 * @Date: 2025-02-18
 * @LastEditors: xuetao 3168581151@qq.com
 * @LastEditTime: 2025-03-06
 * @Description: 
 */
// 获取毫秒级时间戳
import 'package:flutter/material.dart';

getTimestamp() {
  return DateTime.now().millisecondsSinceEpoch;
}

// 根据毫秒级时间戳获取当地时间
DateTime getLocalTime(int timestamp) {
  DateTime utcTime = DateTime.fromMillisecondsSinceEpoch(timestamp);
  return utcTime.toLocal();
}

// 将传入的数字变为至少两位的字符串
String twoDigits(int number) {
  return number.toString().padLeft(2, '0');
}

String getLocalTimeHHMM(int timestamp) {
  DateTime localTime = getLocalTime(timestamp);
  String hour = twoDigits(localTime.hour);
  String minute = twoDigits(localTime.minute);
  return '$hour:$minute';
}

String getLocalTimeDDHHMM(int timestamp) {
  DateTime localTime = getLocalTime(timestamp);
  String month = twoDigits(localTime.month);
  String day = twoDigits(localTime.day);
  String minute = twoDigits(localTime.minute);
  return '$month - $day - $minute';
}

String getLocalTimeYYMMDD(int timestamp) {
  DateTime localTime = getLocalTime(timestamp);
  String year = twoDigits(localTime.year);
  String month = twoDigits(localTime.month);
  String day = twoDigits(localTime.day);
  String minute = twoDigits(localTime.minute);
  return '$year - $month - $day - $minute';
}

// 日期选择器
Future<DateTime?> datePicker({
  required BuildContext context,
  required int? initTime,
  required String helpText,
}) async {
  // 1. 设置初始日期（处理可能为 null 的情况）
  final DateTime initialDate = initTime != null
      ? getLocalTime(initTime)
      : DateTime.now().subtract(const Duration(days: 365 * 18)); // 默认 18 年前

  // 2. 调用日期选择器并返回结果
  return await showDatePicker(
    context: context,
    builder: (context, child) {
      return Theme(
        data: Theme.of(context).copyWith(
          colorScheme: ColorScheme.light(
            primary: Color(0xFF99A8F1), // 选中日期的背景色
            onPrimary: Colors.white, // 选中日期的文字颜色
          ),
          textTheme: TextTheme(
            bodyLarge: TextStyle(color: Colors.grey), // 未选中日期的文字颜色
          ),
        ),
        child: child!,
      );
    },
    initialDate: initialDate,
    firstDate: DateTime(1900), // 允许选择更早的年份
    lastDate: DateTime.now(),
    initialEntryMode: DatePickerEntryMode.calendarOnly,
    initialDatePickerMode: DatePickerMode.year, // 初始显示年份选择
    locale: const Locale('zh', 'CN'),
    helpText: helpText,
    cancelText: '取消',
    confirmText: '确定',
    // 可选：禁用未来日期和今天之后的日期
    selectableDayPredicate: (DateTime date) {
      return date.isBefore(DateTime.now().add(const Duration(days: 1)));
    },
  );
}

// 更具传入的时间来获取当前时间，以及相应的格式
String? checkTimeDifference({int? timestamp}) {
  final int nowTimestamp = getTimestamp();
  const int threeMinutes = 3 * 60 * 1000; // 180000 毫秒
  const int oneHour = 60 * 60 * 1000; // 3600000
  const int oneDay = 24 * oneHour; // 86400000

  if (timestamp == null) return getLocalTimeHHMM(nowTimestamp);

  final difference = (nowTimestamp - timestamp).abs();

  if (difference > oneDay) {
    return getLocalTimeDDHHMM(nowTimestamp);
  } else if (difference > threeMinutes) {
    return getLocalTimeHHMM(nowTimestamp);
  }
  return null;
}
